
#include "SingerCover.h"
#include "../VSingerCoverEffect.h"
#include "../VPicture.h"
#include "../VMain.h"


static int singer_cover_pick_rects
[VSingerCoverEffect::ITEM_PER_PGAE][4] =
{
    {0, 212, 256, 505},
    {257, 212, 481, 505},
    {482, 188, 796, 505},
    {797, 200, 1023, 505},
    {1024, 212, 1280, 505}
};

#ifdef UNDER_CE
static void transform_point(GLfloat out[4], const GLfloat m[16], const GLfloat in[4])
{
#define M(row,col) m[col*4+row]
    out[0] =
        M(0, 0) * in[0] + M(0, 1) * in[1] + M(0, 2) * in[2] + M(0, 3) * in[3];
    out[1] =
        M(1, 0) * in[0] + M(1, 1) * in[1] + M(1, 2) * in[2] + M(1, 3) * in[3];
    out[2] =
        M(2, 0) * in[0] + M(2, 1) * in[1] + M(2, 2) * in[2] + M(2, 3) * in[3];
    out[3] =
        M(3, 0) * in[0] + M(3, 1) * in[1] + M(3, 2) * in[2] + M(3, 3) * in[3];
#undef M
}
// gluProject source code (说明见OpenGL API文档)
static GLint gluProject(GLfloat objx, GLfloat objy, GLfloat objz,
                        const GLfloat  modelMatrix[16], const GLfloat projMatrix[16],
                        const GLint viewport[4], GLfloat* winx, GLfloat* winy,
                        GLfloat* winz)
{
    // matrice transformation
    GLfloat in[4], out[4];
    //initialize matrice and column vector as a transformer
    in[0] = objx;
    in[1] = objy;
    in[2] = objz;
    in[3] = 1.0;
    transform_point(out, modelMatrix, in);  //乘以模型视图矩阵
    transform_point(in, projMatrix, out);   //乘以投影矩阵

    //齐次向量的第四项不能为0
    if (in[3] == 0.0)
    {
        return GL_FALSE;
    }

    //向量齐次化标准化
    in[0] /= in[3];
    in[1] /= in[3];
    in[2] /= in[3];
    //视口向量的作用
    *winx = viewport[0] + (1 + in[0]) * viewport[2] / 2;
    *winy = viewport[1] + (1 + in[1]) * viewport[3] / 2;
    *winz = (1 + in[2]) / 2;
    return GL_TRUE;
}
#endif

SingerCoversDeque g_CoverDeque;

const float CSingerCover::common_w = 1.4f;
const float CSingerCover::common_h = 1.4f;

CSingerCover::CSingerCover(void)
{
    initdata();
}


CSingerCover::CSingerCover(singer_t* singer)
{
    initdata();
    _singer = singer;
}

CSingerCover::~CSingerCover(void)
{
}

singer_t* CSingerCover::Click(int x, int y)
{
    int i = VSingerCoverEffect::ITEM_PER_PGAE;
    int* rect = NULL;

    for (i = 0; i != VSingerCoverEffect::ITEM_PER_PGAE; ++i)
    {
        rect = singer_cover_pick_rects[i];

        if (VMain::inRect(rect[0], rect[1],
                          rect[2], rect[3],
                          x, y))
        {
            if (((VSingerUi*)VMain::queryUi(UI_SINGER))->
                    getCover()->current_position() + (i - 2) >= g_CoverDeque.size()) {
                return NULL;
            }
           
            return (g_CoverDeque[
                        ((VSingerUi*)VMain::queryUi(UI_SINGER))->
                        getCover()->current_position() + (i - 2)
                    ]->_singer);
        }
    }

    return NULL;
}

void CSingerCover::Render(void)
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//    glLoadIdentity();
 
    /* 反N形点
         v        /v
         v      /  v
         v    /    v
         v  /      v
         v/        v
     */
    float  pfVertices[] =
    { 
        -CSingerCover::common_w / 2, CSingerCover::common_h, 0.0f,
        -CSingerCover::common_w / 2, 0.0f, 0.0f,
        CSingerCover::common_w / 2, CSingerCover::common_h, 0.0f,
        CSingerCover::common_w / 2, 0, 0.0f
    };

    static const GLfloat texCoords[] =
    {
        0, 0,
        0, 1,
        1, 0,
        1, 1
    };

    glPushMatrix();
    glTranslatef(m_v3Position.x, m_v3Position.y, m_v3Position.z);
    glRotatef(m_fAngle, 0, 1, 0);

    glBindTexture(GL_TEXTURE_2D, _singer->texture);
    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
    glVertexPointer(3, GL_FLOAT, 0, pfVertices);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    RenderDetail();
    RenderText();
#if 1

    if (1)//画倒影
    {

        float  pfVertices1[] =
        {
            -CSingerCover::common_w / 2, 0, 0.0f,
            -CSingerCover::common_w / 2, -CSingerCover::common_h* .5 , 0.0f,
            CSingerCover::common_w / 2, 0, 0.0f,
            CSingerCover::common_w / 2, -CSingerCover::common_h* .5 , 0.0f
        };

        static const GLfloat texCoords1[] =
        {
            0, 1,
            0, 0,
            1, 1,
            1, 0
        };

        GLfloat color[] =
        {
            0.6f,  0.6f,  0.6f,  1.0f,
            0.0f,  0.0f,  0.0f,  1.0f,
            0.6f,  0.6f,  0.6f,  1.0f,
            0.0f,  0.0f,  0.0f,  1.0f
        };

        glEnableClientState(GL_COLOR_ARRAY);
        glBindTexture(GL_TEXTURE_2D, _singer->texture);
        glColorPointer(4, GL_FLOAT, 0, color);
        glTexCoordPointer(2, GL_FLOAT, 0, texCoords1);
        glVertexPointer(3, GL_FLOAT, 0, pfVertices1);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        glDisableClientState(GL_COLOR_ARRAY);
    }

#endif
    glPopMatrix();
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
void CSingerCover::initdata()
{

    m_fAngle     = 0;
    _singer = NULL;
}


void CSingerCover::RenderText()
{
    static float  pfVertices[] =
    {
        -CSingerCover::common_w / 2 + 0.2f, CSingerCover::common_h - 0.03f, 0.0f,
        -CSingerCover::common_w / 2 + 0.2f, CSingerCover::common_h - 0.2f, 0.0f,
        CSingerCover::common_w / 2 - 0.2f, CSingerCover::common_h - 0.03f, 0.0f,
        CSingerCover::common_w / 2 - 0.2f, CSingerCover::common_h - 0.2f, 0.0f
    };

    static const GLfloat texCoords[] =
    {
        0, 0,
        0, 1,
        1, 0,
        1, 1
    };
    glBindTexture(GL_TEXTURE_2D, _singer->captexture);
    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
    glVertexPointer(3, GL_FLOAT, 0, pfVertices);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

void CSingerCover::RenderDetail()
{
    static float  pfVertices[] =
    {
        -CSingerCover::common_w / 2 + 0.05f, CSingerCover::common_h - 0.03f, 0.0f,
        -CSingerCover::common_w / 2 + 0.05f, CSingerCover::common_h - 0.2f, 0.0f,
        CSingerCover::common_w / 2 - 1.2f, CSingerCover::common_h - 0.03f, 0.0f,
        CSingerCover::common_w / 2 - 1.2f, CSingerCover::common_h - 0.2f, 0.0f
    };

    static const GLfloat texCoords[] =
    {
        0, 0,
        0, 1,
        1, 0,
        1, 1
    };
    glBindTexture(GL_TEXTURE_2D, _singer->texture);
    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
    glVertexPointer(3, GL_FLOAT, 0, pfVertices);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}